package com.example.exercise.xiaodemos;

import org.springframework.util.FileSystemUtils;

import java.io.File;

public class MyFileUtils {
    public static void main(String[] args) {
        File file = new File("E:/java-spring/demo-springboot");
        deleteTargetOrLogFile(file);
    }

    public static void deleteTargetOrLogFile(File file) {
        String fileName = file.getName();
        if(file.isDirectory()) {
            if("target".equals(fileName) || "log".equals(fileName) || "logs".equals(fileName)) {
                System.out.println(FileSystemUtils.deleteRecursively(file));
            } else {
                File[] files = file.listFiles();
                if(files != null && files.length > 0) {
                    for (File f : files) {
                        deleteTargetOrLogFile(f);
                    }
                }
            }
        } else {
            if(fileName.endsWith(".log") || fileName.endsWith(".iml")) {
                System.out.println(file.delete());
            }
        }
    }
}
